<!--
 * @Author: hl
 * @Date: 2023-07-11 13:52:31
 * @LastEditors: lh
 * @LastEditTime: 2023-10-20 15:57:22
-->
<template>
  <div>
    <!-- :searchDataList="searchDataListJy" -->
    <tabCom
      @Refresh="Refresh"
      @search-callback="searchCallback"
      :title="'所有请假审批'"
      :operation-api="operationApiA"
      :table-data-rule="tableDataRuleA"
      :search-data-list="searchDataListJy"
      :needPagination="true"
      :update_show_key="'id'"
      :update_show_value="[0]"
      :formDataWidth="'30%'"
      :updateShow="false"
      :deleteShow="false"
      :addButtonList="addButtonList"
      @buttonMoudle="buttonMoudleFn"
      :isShowAddBtn="false"
      :showSearch="false"
      :isShowDownLoadFile="false"
    >
      <template #add="{ formData }">
        <el-form-item :label="item.title" v-for="(item, index) in searchDataListJyEdit" :key="index">
          <el-input v-if="!item.type" v-model="formData[item.name]" placeholder="请输入" />
          <el-input
            v-else-if="item.type == 'number'"
            type="number"
            v-model="formData[item.name]"
            placeholder="请输入"
          />
          <!-- value-format="YYYY/MM/DD" -->
          <el-date-picker
            v-else-if="item.type == 'timeday'"
            v-model="formData[item.name]"
            type="date"
            format="YYYY/MM/DD"
            placeholder="请选择"
          />
          <el-date-picker
            v-else-if="item.type == 'datetime'"
            v-model="formData[item.name]"
            type="datetime"
            format="YYYY/MM/DD HH:mm:ss"
            placeholder="请选择"
          />
          <upfilebox
            v-else-if="item.type == 'file'"
            fileType="file"
            v-model="formData[item.name]"
            v-model:name="formData[item.prop]"
          ></upfilebox>
          <el-date-picker
            @change="chooseTimeChange(formData[item.name])"
            v-else-if="item.type == 'time'"
            v-model="formData[item.name]"
            type="datetimerange"
            format="YYYY/MM/DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <el-select
            v-else-if="item.type == 'select' && item!.list!.length > 0"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            allow-create
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.value || items.id"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'selectLable'"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'multipleSelect'"
            multiple
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
        </el-form-item>
      </template>
    </tabCom>
    <el-dialog v-model="dialogVisible" title="审批" width="30%" :before-close="handleClose">
      <el-form ref="ruleFormRef" label-width="120px" class="demo-ruleForm" status-icon>
        <el-form-item label="姓名" prop="desc">
          <el-input v-model="tableDataRow.userName" disabled />
        </el-form-item>
        <el-form-item label="请假类型" prop="desc">
          <el-input v-model="tableDataRow.leaveType" disabled />
        </el-form-item>
        <el-form-item label="请假原因" prop="desc">
          <el-input v-model="tableDataRow.staffSignature" type="textarea" disabled />
        </el-form-item>
        <el-form-item label="是否同意" prop="resource">
          <el-radio-group v-model="ruleForm.isRatify">
            <el-radio :label="1">同意</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="意见" prop="desc">
          <el-input v-model="ruleForm.opinion" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isAgreewithFn(2)">取消</el-button>
          <el-button type="primary" @click="isAgreewithFn(1)"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import tabCom from "@/components/table/indexcopy.vue"
// import { gethomeStatistics } from "@/api/home/index"
import { ref, onMounted, reactive } from "vue"
import upfilebox from "@/components/table/upfile.vue"
import { ElMessageBox, ElMessage } from "element-plus"
import { ratifyLeave } from "@/api/leaveapp"
import type { FormInstance, FormRules } from "element-plus"

onMounted(() => {
  init()
})
// 时间选择限定范围
const chooseTimeChange = (time: any) => {
  const starTime = JSON.parse(JSON.stringify(time[0]))
  const endTime = JSON.parse(JSON.stringify(time[1]))
  const minTime = new Date(insertStr(insertStr(starTime, 4, "-"), 7, "-")).getTime()
  const maxTime = new Date(insertStr(insertStr(endTime, 4, "-"), 7, "-")).getTime()
  const allow = 2592000000 //30天
  if (maxTime - minTime > allow) {
    ElMessage.warning("选择范围请在30天以内!")
    chaxunState.value = false
  } else {
    chaxunState.value = true
  }
}
// 接口请求配置
const operationApiA = ref<any>([
  // { type: "add", interface: "/dangerousWork/insertOrUpdate" },
  // { type: "update", interface: "/dangerousWork/insertOrUpdate" },
  { type: "search", interface: "/staffLeave/selectExamineAll" },
  // { type: "delete", interface: "/dangerousWork/deleteByIds" }
  { type: "examineFn", interface: "" }
])
type addType = {
  name: string
  type: string
}
const addButtonList = ref<addType[]>([
  // {type:'examineFn',name:'审核'}
])

// 搜索配置
const searchDataListJy = [{ name: "id", title: "id" }]
// 添加配置
const searchDataListJyEdit = [
  { name: "constructionActivities", title: "施工活动" },
  { name: "constructionUnit", title: "施工单位" },
  { name: "programmeName", title: "方案名称	" },
  { name: "programmeNum", title: "方案编号" },
  { name: "scale", title: "规模" },
  {
    name: "constructionProgress",
    title: "施工进展",
    type: "select",
    list: [
      { value: 0, label: "未开始", name: "未开始", id: 0 },
      { value: 1, label: "施工中", name: "施工中", id: 1 },
      { value: 2, label: "已完成", name: "已完成", id: 2 }
    ]
  },
  { name: "supervisionImplementationRulesUrl", title: "监理实施细则", type: "file" },
  { name: "specialConstructionPlanReviewRecordUrl", title: "专项施工方案审查记录", type: "file" },
  { name: "specialInspectionRecordUrl", title: "专项巡视检查记录", type: "file" },
  { name: "acceptanceDataUrl", title: "验收资料", type: "file" },
  { name: "rectificationDataUrl", title: "整改资料", type: "file" },
  { name: "responsible", title: "责任人", type: "user" },
  { name: "remarks", title: "备注" }
]
// 表单配置
const tableDataRuleA = ref([
  { prop: "id", label: "id" },
  { prop: "leaveType", label: "请假类型" },
  { prop: "leaveReason", label: "请假原因" },
  {
    prop: "leaveStatus",
    label: "请假状态",
    type: "select",
    list: [
      { value: 0, label: "编辑", name: "编辑", id: 0 },
      { value: 1, label: "内设机构待同意", name: "内设机构待同意", id: 1 },
      { value: 2, label: "部门总监待同意", name: "部门总监待同意", id: 2 },
      { value: 3, label: "待填销假时间", name: "待填销假时间", id: 3 },
      { value: 4, label: "待审核并批准销假", name: "待审核并批准销假", id: 4 },
      { value: 5, label: "审核并批准同意", name: "审核并批准同意", id: 5 },
      { value: 6, label: "驳回", name: "驳回", id: 6 }
    ]
  },
  { prop: "projectName", label: "项目名称	" },
  { prop: "leaveStartTime", label: "请假时间", type: "time" },
  { prop: "reportingTime", label: "销假时间", type: "time" },
  { prop: "internalOrganizationUserName", label: "内设机构审批人" },
  { prop: "internalOrganizationOpinion", label: "内设机构意见" },
  { prop: "departmentUserName", label: "部门总监" },
  { prop: "departmentOpinion", label: "部门意见" },
  { prop: "userName", label: "用户姓名" },
  { prop: "residueRestDays", label: "剩余可调休/轮休天数" },
  { prop: "restDays", label: "已调休/轮休天数" },
  { prop: "staffSignature", label: "员工签字" },
  { prop: "remarks", label: "备注" }
  // {prop: 'operate',label: '操作'},
])
// 初始化
const init = () => {}

//#region 审核按钮

let dialogVisible = ref<boolean>(false) // 审核弹框
let tableDataRow = ref<any>({}) // 当前审核数据

// × 掉弹框回调
const handleClose = () => {
  ElMessageBox.confirm(
    `${tableDataRow.value.userName}的${tableDataRow.value.leaveType}审批`,
    `确定暂不处理${tableDataRow.value.userName}审批`,
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(() => {
      dialogVisible.value = false
    })
    .catch(() => {})
}

// 表单ref
const ruleFormRef = ref<FormInstance>()
interface RuleForm {
  isRatify: string | number
  opinion: string
}
const ruleForm = reactive<RuleForm>({
  isRatify: 1,
  opinion: ""
})

// 处理 同意和驳回 方法
const isAgreewithFn = async (type: number) => {
  console.log("submit!", ruleForm)
  if (type == 1) {
    // 确认
    if (!ruleForm.opinion) {
      ElMessage.error("请输入审批意见")
      return
    }
    ratifyLeave({
      ...ruleForm,
      id: tableDataRow.value.id,
      ratifyType: tableDataRow.value.leaveStatus
    })
      .then((res) => {
        console.log(res)
        if (res.code == 200) {
          ElMessage("审批成功")
          ruleForm.isRatify = 1
          ruleForm.opinion = ""
          dialogVisible.value = false
        }
      })
      .catch((err) => {})
  } else {
    // 取消
    handleClose()
  }
}

// 按钮回调
const buttonMoudleFn = (type: string, row: any, Face = "") => {
  console.log(type, row, Face, "--buttonMoudleFn--")
  let leaveapp_status = row.leaveStatus
  let arr = [1, 2, 4]
  if (!arr.includes(leaveapp_status)) {
    return
  }
  dialogVisible.value = true
  tableDataRow.value = row
  // ElMessageBox.confirm(
  //   `${row.userName}的${row.leaveType}审批`,
  //   `提示`,
  //   {
  //     confirmButtonText: '确认',
  //     cancelButtonText: '取消',
  //     type: 'warning',
  //   }
  // ).then(() => {

  // }).catch(() => {

  // })
}

//#endregion

// 表单刷新
const Refresh = () => {
  //用户数据概览
}
// 搜索回调
const searchCallback = (search: any) => {}
const loading = ref<boolean>(false)
const gailan = ref<any>({})
const gethomeStatisticsStar = (obj: any = {}) => {
  // loading.value = true
  // gethomeStatistics(obj).then((res: any) => {
  //   gailan.value = res.data
  //   loading.value = false
  // })
}
</script>
<style scoped lang="scss">
.search-wrapper {
  margin: 0 auto;
  width: 98.5%;
  margin-top: 10px;
}
</style>
